package com.ys.web.mobao.manager;

import com.ys.utils.base.j2se.Logger;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.web.mobao.model.po.SystemBankPO;
import com.ys.web.mobao.service.MobaoBindCardService;
import com.ys.web.mobao.vo.SystemBankVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: dhf
 * @ClassName: BankManager
 * @Date: 2022/8/15 11:00
 * @Description:
 * @Version: 1.0
 */
@Service
public class BankManager implements Runnable{

    @Autowired
    private MobaoBindCardService mobaoBindCardService;
    private static List<SystemBankVo> list = new ArrayList<>();

    private static List<SystemBankVo> listMb = new ArrayList<>();

    private static List<SystemBankVo> listhjzf = new ArrayList<>();
    private static List<SystemBankPO> listPo = new ArrayList<>();
    private static Map<String, SystemBankPO> systemBankMap = new HashMap<>();
    @PostConstruct
    public void init () {
        new Thread(this).start();
    }
    @Override
    public void run() {
        while (true) {
            try {
                this.load();
                Thread.sleep(5*60*1000);
            } catch (Exception e) {
                Logger.error("BankManager",  e);
            }
        }
    }

    private void load(){
        List<SystemBankPO> list = mobaoBindCardService.getAll();
        if(StringHandler.isNotEmpty(list)){
            listPo = list;
            List<SystemBankVo> list1 = new ArrayList<>();
            List<SystemBankVo> list2 = new ArrayList<>();
            Map<String, SystemBankPO> systemBank = new HashMap<>();
            for (SystemBankPO systemBankPO:list) {
                list1.add(systemBankPO.systemBankVo());
                systemBank.put(systemBankPO.bankName,systemBankPO);
            }
            for (SystemBankPO systemBankPO:list) {
                list2.add(systemBankPO.systemBankHjzfVo());
            }
            this.list = list1;
            this.listhjzf = list2;
            this.systemBankMap = systemBank;
        }
        List<SystemBankPO> listMb = mobaoBindCardService.getAllMb();
        if(StringHandler.isNotEmpty(list)){
            List<SystemBankVo> list1 = new ArrayList<>();
            for (SystemBankPO systemBankPO:listMb) {
                list1.add(systemBankPO.systemBankVoMb());
            }
            this.listMb = list1;
        }
        Logger.info("BankManager---------------加载完成");
    }

    public static List<SystemBankVo> list(String payChannel){
        if("ys".equals(payChannel)){
            return list;
        }
        if("mb".equals(payChannel)){
            return listMb;
        }
        if("hjzf".equals(payChannel)){
            return listhjzf;
        }
        return list;
    }

    public static SystemBankPO SystemBank(String name){
        if(StringHandler.isNotEmpty(systemBankMap.get(name))){
            return systemBankMap.get(name);
        }else{
            SystemBankPO SystemBankVo = null;
            for (SystemBankPO systemBankPO:listPo) {
                if(name.contains(systemBankPO.bankName)){
                    SystemBankVo = systemBankPO;
                    break;
                }
            }
            return SystemBankVo;
        }
    }
}
